package com.fzz.learning_forum.serviceImpl;

import com.fzz.learning_forum.dao.CommentMapping;
import com.fzz.learning_forum.domain.Article;
import com.fzz.learning_forum.domain.Comment;
import com.fzz.learning_forum.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapping commentMapping;

    //发表评论
    @Override
    public Integer publishComment(Comment comment) {

        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mmm:ss");
        simpleDateFormat.format(date);
        comment.setCreated(date);
        return commentMapping.publishComment(comment);
    }

    //分页查询评论信息
    @Override
    public PageInfo<Comment> selectCommentWithPage(Integer article_id,Integer page,Integer count) {

        PageHelper.startPage(page,count);
        List<Comment> commentList = commentMapping.selectCommentWithPage(article_id);
        PageInfo<Comment> pageInfo=new PageInfo<>(commentList);
        return pageInfo;
    }

    //查询我评论过的动态
    @Override
    public PageInfo<Article> myCommentArticle(Integer id, Integer page, Integer count) {
        PageHelper.startPage(page,count);
        List<Article> articleList = commentMapping.myCommentArticle(id);
        PageInfo<Article> pageInfo=new PageInfo<>(articleList);
        return pageInfo;
    }

    @Override
    public List<Comment> selectNewComment(Integer id) {
        return commentMapping.selectNewComment(id);
    }
}
